import React from 'react'
import Logo from '../../component/logo/logo'
import { WingBlank, WhiteSpace, Button, InputItem, List } from 'antd-mobile';
import { Redirect } from 'react-router-dom'

//使用react-redux 实现数据连接  在组件中直接引入 reducer 文件
import { connect } from 'react-redux'
import { login } from '../../redux/user_redux'

@connect(
    state => state.user,
    { login }
)

class Login extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            user: '',
            pwd: ''
        }

        //所有的绑定事件 性能优化
        this.register = this.register.bind(this)
        this.handleLogin = this.handleLogin.bind(this)
    }

    register() {
        // console.log(this.props) 可查看相关路由属性
        this.props.history.push('/register')
    }

    handleLogin() {
        this.props.login(this.state)
    }
    handleChange(key, value) {
        this.setState({
            [key]: value
        })
    }

    render() {
        return (
            <div>
                {this.props.redirectTo ? <Redirect to={this.props.redirectTo} /> : null}
                <Logo />
                <WingBlank>
                    <List>
                        {this.props.msg ? <p className="error_msg">{this.props.msg}</p> : null}
                        <InputItem
                            onChange={item => this.handleChange('user', item)}
                        >用户名</InputItem>
                        <WhiteSpace />
                        <InputItem
                            onChange={item => this.handleChange('pwd', item)}
                            type="password"
                        >密码</InputItem>
                    </List>
                    <Button type="primary" onClick={this.handleLogin}>登陆</Button>
                    <WhiteSpace />
                    <Button type="primary" onClick={this.register}>注册</Button>
                </WingBlank>
            </div>
        )
    }
}


export default Login